package com.wfw.feichat.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

/**
 * @author: wfw
 * @date: 2019-05-21 16:29
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
public class ServerResponse<T> implements Serializable {

    private static final long serialVersionUID = -2683167061899090458L;
    private Integer code;
    private String msg;
    private T data;

    private ServerResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ServerResponse(T data) {
        this.code = ResponseCode.OK.getCode();
        this.msg = ResponseCode.OK.getMsg();
        this.data = data;
    }

    public static ServerResponse buildOk() {
        return new ServerResponse<>(null);
    }

    public static <T> ServerResponse<T> buildOk(T data) {
        return new ServerResponse<>(data);
    }

    public static ServerResponse buildError() {
        return new ServerResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg());
    }

    public static ServerResponse buildErrorMsg(String msg) {
        return new ServerResponse<>(ResponseCode.ERROR.getCode(), msg);
    }

    public static ServerResponse buildErrorCodeMsg(Integer code, String msg) {
        return new ServerResponse<>(code, msg);
    }

    public static ServerResponse buildErrorRC(ResponseCode rc) {
        return new ServerResponse<>(rc.getCode(), rc.getMsg());
    }
}
